package com.tjetc.view.adminview;

import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.LeaveMsg;
import com.zhiyun.service.LeaveMsgService;
import com.zhiyun.vo.AppData;
import com.zhiyun.vo.Result;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Admins_Mails {
    LeaveMsgService leaveMsgService= CilentProxy.getProxyInstance(LeaveMsgService.class);
    Scanner scanner=new Scanner(System.in);
    public void start() {
        while (true){
            System.out.println("~~~~~~~~~留言~~~~~~~~~");
            System.out.println("1. 所有留言");
            System.out.println("2. 回复留言");
            System.out.println("0. 退出");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    allLeaveMsg();
                    break;
                case 2:
                    replyLeaveMsg();
                    break;
                case 0:

                    return;
                default:
                    break;
            }
        }
    }

    private void replyLeaveMsg() {
        while (true){
            Result result = leaveMsgService.listLeaveMsg();
            if (result.getCode()==200){
                List<LeaveMsg> obj = (List<LeaveMsg>) result.getObj();
                for (LeaveMsg leaveMsg : obj) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                    // 将Date转换为String
                    String dateString = sdf.format(leaveMsg.getCreateTime());
                    System.out.println(leaveMsg.getId()+"    内容:"+leaveMsg.getMsg()+"       发布时间"+dateString);
                    if (leaveMsg.getAdminId()!=null){
                        String format = sdf.format(leaveMsg.getResolveTime());
                        System.out.println("回复管理员id"+leaveMsg.getAdminId()+"   内容"+leaveMsg.getResponseMsg()+"    回复时间"+format);
                    }
                }
                System.out.println("请选择要回复的留言id");
                int i = scanner.nextInt();
                LeaveMsg leaveMsg=null;
                for (LeaveMsg leaveMsgs : obj) {
                    if (i==leaveMsgs.getId()){
                        leaveMsg=leaveMsgs;
                    }
                }
                System.out.println("请输入回复的内容");
                String next = scanner.next();
                leaveMsg.setAdminId(AppData.admin.getId());
                leaveMsg.setResponseMsg(next);
                leaveMsg.setResolveTime(new Date());
                Result result1 = leaveMsgService.updateLeaveMsg(leaveMsg);
                if (result1.getCode()==200){
                    System.out.println(result1.getMsg());
                }else {
                    System.out.println(result1.getMsg());
                }
            }else {
                System.out.println(result.getMsg());
            }
            System.out.println("是否继续回复留言(Y/N)");
            String next = scanner.next();
            if (next.equals("Y")){

            }else {
                break;
            }
        }
    }

    private void allLeaveMsg() {
        while (true){
            Result result = leaveMsgService.listLeaveMsg();
            if (result.getCode()==200){
                List<LeaveMsg> obj = (List<LeaveMsg>) result.getObj();
                for (LeaveMsg leaveMsg : obj) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                    // 将Date转换为String
                    String dateString = sdf.format(leaveMsg.getCreateTime());
                    System.out.println(leaveMsg.getId()+"    内容:"+leaveMsg.getMsg()+"       发布时间"+dateString);
                    if (leaveMsg.getAdminId()!=null){
                        String format = sdf.format(leaveMsg.getResolveTime());
                        System.out.println("回复管理员id"+leaveMsg.getAdminId()+"   内容"+leaveMsg.getResponseMsg()+"    回复时间"+format);
                    }
                }
            }else {
                System.out.println(result.getMsg());
            }
            break;
        }
    }
}
